package com.oracle.cmp.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.oracle.cmp.common.Constant;
import com.oracle.cmp.common.PageInfo;
import com.oracle.cmp.common.Result;
import com.oracle.cmp.entity.Customer;
import com.oracle.cmp.entity.Parts;
import com.oracle.cmp.service.CustomerService;
import com.oracle.cmp.service.CustomerServiceImpl;
import com.oracle.cmp.service.PartsService;
import com.oracle.cmp.service.PartsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/parts.do")
public class PartsController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String op = request.getParameter("op");
        if("query".equals(op)){
            query(request,response);
        }else if("delete".equals(op)){
            delete(request,response);
        }else if("edit".equals(op)){
            edit(request,response);
        }else if("update".equals(op)){
            update(request,response);
        }else if("add".equals(op)){
            add(request,response);
        }else if("save".equals(op)){
            save(request,response);
        }else if("queryajax".equals(op)){
       queryajax(request,response);
    }

    }

    public void query(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
       PartsService partsService = new PartsServiceImpl();
        String partsName = request.getParameter("partsName");
        String pageNo = request.getParameter("pageNum");
        int pageNum = 1;
        if(pageNo!=null)
            pageNum = Integer.parseInt(pageNo);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("partsName",partsName);
        Result result =partsService.queryForPage(map,pageNum);
        int total = partsService.count(map);
        int pages = (total+ Constant.PAGESIZE-1)/Constant.PAGESIZE;
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList((List)result.getData());
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(Constant.PAGESIZE);
        pageInfo.setTotal(total);
        pageInfo.setPages(pages);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("partsName",partsName);
        request.getRequestDispatcher("/page/partssys/parts/partslist.jsp").forward(request,response);
    }
    /**
     * 新增页面跳转
     * @return
     */
    public void add(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/page/partssys/parts/partsadd.jsp").forward(request,response);
    }

    /**
     * 新增保存
     * @return
     */

    public void save(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        PartsService partsService=new PartsServiceImpl();
        String partsname = request.getParameter("partsname");
        String  partsmodel= request.getParameter("partsmodel");
        String partsloc = request.getParameter("partsloc");
        String partsprodate = request.getParameter("partsprodate");
        String partsremark=request.getParameter("partsremark");
       Parts parts=new Parts();
        parts.setPartsname(partsname);
        parts.setPartsmodel(partsmodel);
        parts.setPartsloc(partsloc);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        try {
            parts.setPartsprodate(format.parse(partsprodate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        parts.setPartsremark(partsremark);
        partsService.save(parts);
        response.sendRedirect(request.getContextPath()+"/parts.do?op=query");
    }
    /**
     * 删除客户

     * @return
     */

    public void delete(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
      PartsService partsService=new PartsServiceImpl();
        String partsid = request.getParameter("partsid");
       partsService.delete(Integer.parseInt(partsid));
        request.getRequestDispatcher("/parts.do?op=query").forward(request,response);
    }
    /**
     * 进入客户更新页面
     * @return
     */
    public void edit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        PartsService partsService=new PartsServiceImpl();
        String partsid = request.getParameter("partsid");
        Result result = partsService.selectById(Integer.parseInt(partsid));
        request.setAttribute("parts", result.getData());
        request.getRequestDispatcher("/page/partssys/parts/partsedit.jsp").forward(request,response);
    }
    /**
     * 客户更新
     * @return
     */
    public void update(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        PartsService partsService=new PartsServiceImpl();
        String partsname = request.getParameter("partsname");
        String  partsmodel= request.getParameter("partsmodel");
        String partsloc = request.getParameter("partsloc");
        String partsprodate = request.getParameter("partsprodate");
        String partsremark=request.getParameter("partsremark");
        String partsid = request.getParameter("partsid");
        Parts parts=new Parts();
        parts.setPartsname(partsname);
        parts.setPartsmodel(partsmodel);
        parts.setPartsloc(partsloc);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        try {
            parts.setPartsprodate(format.parse(partsprodate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        parts.setPartsremark(partsremark);
        parts.setPartsid(Integer.parseInt(partsid));
        partsService.update(parts);
        response.sendRedirect(request.getContextPath()+"/parts.do?op=query");
    }
    public void queryajax(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        PartsService partsService=new PartsServiceImpl();
        String partsname=request.getParameter("partsname");
        List<Parts>list=partsService.querajax(partsname);
        response.getWriter().print(new ObjectMapper().writeValueAsString(list));
    }
}
